package com.xwaf.platform.system.provider;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.xwaf.platform.common.api.entity.netty.WebSocketNettyUsers;
import com.xwaf.platform.common.mqtt.annotation.EnableMqtt;
import com.xwaf.platform.common.mqtt.annotation.EnableMqttMq;
import com.xwaf.platform.system.api.service.gateway.HttpService;

/**
 * 
 * <p>
 * Spring-boot 启动入口
 * <p>
 * 
 * @author 李聪 <br>
 * @email xwaf_love@yeah.net <br>
 * @since JDK 1.8<br>
 * @date 2019年12月27日 下午7:57:15 <br>
 * @see 无<br>
 *      Copyright (c) 2019, xwaf_love@yeah.net All Rights Reserved.<br>
 */
@RestController
@ServletComponentScan
@ComponentScan(basePackages = { "com.xwaf" })
@SpringBootApplication(exclude = { SecurityAutoConfiguration.class })
@EnableTransactionManagement // 启用事务
@ImportResource("classpath:dubbo-*provider.xml")
@EnableScheduling
@EnableAsync // 启用异步
@EnableMqtt // mqtt
@EnableMqttMq
public class SysProviderApplication {

	private static final Logger LOGGER = LoggerFactory.getLogger(SysProviderApplication.class);

	@Autowired
	HttpService httpService;

	@RequestMapping
	public String hello() {
		return "Hello World!";
	}

	// 健康检查
	@RequestMapping("/check")
	public String check() {
		return "success";
	}

	@RequestMapping("/httpPost")
	public String httpPost() {
		Map<String, Object> reqMap = new HashMap<>();
		Map<String, Object> headers = new HashMap<>();
		// headers.put("Content-Type", "application/json");
		// headers.put("charset", "utf-8");
		String reqBody = JSON.toJSONString(reqMap);
		String requestHttpPost = httpService.requestHttpPost("/log", headers, reqBody);
		System.out.println(requestHttpPost);
		return "success";
	}

	@RequestMapping("/httpGet")
	public String httpGet() {
		Map<String, Object> reqMap = new HashMap<>();
		Map<String, Object> headers = new HashMap<>();
		// headers.put("Content-Type", "application/json");
		// headers.put("charset", "utf-8");
		String reqBody = JSON.toJSONString(reqMap);
		String requestHttpPost = httpService.requestHttpGet("/log", headers, reqBody);
		System.out.println(requestHttpPost);
		return "success";
	}

	// 日志等级处理
	// http://localhost:19101/log
	@RequestMapping("/log")
	public String log() {
		LOGGER.info("log info");
		LOGGER.warn("log warn");
		LOGGER.debug("log debug");
		LOGGER.error("log error");
		return "log success";
	}

	/**
	 * 
	 * <p>
	 * netty 服务
	 * <p>
	 * 
	 * @author 李聪 <br>
	 * @email xwaf_love@yeah.net <br>
	 * @since JDK 1.8<br>
	 * @date 2020年1月9日 下午8:48:54 <br>
	 * @see 无<br>
	 * @param cid
	 * @param message
	 * @return
	 * @demo <br>
	 */
	@RequestMapping("/nettyPush/{userName}")
	public String nettyPushToWeb(@PathVariable String userName, String message) {
		WebSocketNettyUsers.sendMessageToUser(userName, message);
		return userName;
	}
	
	@PostConstruct
	void started() {
		TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
	}

	/**
	 * 
	 * <p>
	 * The entry point of application.
	 * <p>
	 * 
	 * @author 李聪 <br>
	 * @email xwaf_love@yeah.net <br>
	 * @since JDK 1.8<br>
	 * @date 2019年12月30日 上午9:11:36 <br>
	 * @see 无<br>
	 * @param args
	 *            the input arguments
	 * @throws IOException
	 *             the io exception
	 * @demo <br>
	 */
	public static void main(String[] args) throws IOException {
		SpringApplication application = new SpringApplication(SysProviderApplication.class);
		application.setRegisterShutdownHook(false);
		application.setBannerMode(Banner.Mode.OFF);
		ConfigurableApplicationContext context = application.run(args);
		// 可以 验证我们的bean是否成功注入
		// context.getBean("auth");
		// application.run(args);
		Assert.notNull(true, "main started");// 可用於錯誤处理
		LOGGER.info("platform-system-provider started!!!");
	}
}
